package yunhe.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;

import yunhe.biz.IProcureBiz;
import yunhe.biz.impl.ProcureBizImpl;
import yunhe.entity.PurchaseAppend;
import yunhe.util.GeneralUtil;
@WebServlet(name="SelectValidPurchaseServlet",urlPatterns="/SelectValidPurchaseServlet")
public class SelectValidPurchaseServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String pageIndex=request.getParameter("page");
		if(pageIndex==null){
			pageIndex="1";
		}
		int currPage=Integer.parseInt(pageIndex);
		String pagesize=request.getParameter("rows");
		int pageSize=Integer.parseInt(pagesize);
		IProcureBiz ipb=new ProcureBizImpl();
		int count=ipb.selectValidPurQuantity();
		int totalPages=GeneralUtil.getTotalPages(pageSize, count);
		if(currPage<1){
			currPage=1;
		}else if(currPage>totalPages){
			currPage=totalPages;
		}
		List<PurchaseAppend> purchaseAppendList=ipb.selectPurchaseAppendByValidPurQuantity(pageSize, currPage);
		JSONArray ja=new JSONArray();
		for(int i=0;i<purchaseAppendList.size();i++){
			PurchaseAppend pa=purchaseAppendList.get(i);
			Map<String,Object> inMap=new HashMap<String,Object>();
			inMap.put("pursId", pa.getPurchase().getPursId());
			inMap.put("supName",pa.getPurchase().getSupplier().getSupName());
			inMap.put("purchaseDate",pa.getPurchase().getPutchaseDate());
			inMap.put("operatorName",pa.getPurchase().getOperator().getName());
			inMap.put("docMakerName", pa.getPurchase().getDocMaker().getName());
			inMap.put("checkerName", pa.getPurchase().getChecker().getName());
			inMap.put("keeperName", pa.getPurchase().getKeeper().getName());
			inMap.put("accountPaying", pa.getPurchase().getAccountPaying());
			inMap.put("accountPaied", pa.getPurchase().getAccountPaied());
			inMap.put("executeState", pa.getPurchase().getExcuteState());
			/*inMap.put("proName", pa.getProduct().getProName());
			inMap.put("purQuantity", pa.getPurQuantity());
			inMap.put("purPrice", pa.getPurPrice());
			inMap.put("purDiscount", pa.getPurDiscount());
			inMap.put("purTotal", pa.getPurTotal());
			inMap.put("stoName", pa.getStorage().getStoName());*/
			ja.put(inMap);
		}
         response.getWriter().print(ja.toString());
		/*if(purchaseAppendList!=null){
			request.setAttribute("currPage", currPage);
			request.setAttribute("totalPages", totalPages);
			request.setAttribute("purchaseAppendList", purchaseAppendList);
			request.getRequestDispatcher("/purchase/validPurchaseRecord.jsp").forward(request, response);
		}*/
		
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		doGet(req, resp);
	}
}
